package com.eianiao.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eianiao.bean.Category;
import com.eianiao.bean.Employee;
import com.eianiao.common.PageParam;
import com.eianiao.common.R;

import com.eianiao.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author eianiao
 * @Date 2022 06
 * @Version 1.0
 **/
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService cs;

    /**
     * 添加菜品
     * @param category
     * @return
     */
    @PostMapping
    public R add(@RequestBody Category category){

        int rows = cs.add(category);
        if (rows > 0) {
            return R.success("添加成功！");
        } else {
            return R.error("添加失败！");
        }
    }

    /**
     * 查询菜品
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam){
        IPage<Category> page = cs.findPage(pageParam);

        return R.success(page);

    }


    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R delete(long id){
        int rows = cs.delete(id);
        if (rows > 0) {
            return R.success("删除分类成功！");
        } else {
            return R.error("删除分类失败！");
        }
    }


    /**
     * 更新菜品分类
     * @param category
     * @return
     */
    @PutMapping
    public R update(@RequestBody Category category){

        int rows = cs.update(category);
        if (rows > 0) {
            return R.success("更新成功！");
        } else {
            return R.error("更新失败！");
        }
    }


    @GetMapping("/list")
    public R findByType(Integer type){
        List<Category> list = cs.findByType(type);
        return R.success(list);
    }
}
